package com.zpark.servlet_filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;

/**
 * 获取初始化参数
 */
@WebFilter(
        urlPatterns = {"/parm.do", "/sfd03.html"},
        initParams = {
                @WebInitParam(name = "username", value = "张三")
        },
        filterName = "ServletFilterDemo03"
)
public class ServletFilterDemo03 implements Filter {
    private FilterConfig config;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 获取FilterConfig对象
        this.config = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 获取初始化参数
        String username = this.config.getInitParameter("username");
        System.out.println(username);
        System.out.println(this.config.getFilterName());
        System.out.println(this.config.getServletContext());
    }

    @Override
    public void destroy() {

    }
}
